package com.mrd.shiptask_shipowner.fragments;

import java.util.HashMap;
import java.util.Map;
import com.mrd.shiptask_shipowner.R;
import com.mrd.shiptask_shipowner.activity.NewsActivity;
import com.mrd.shiptask_shipowner.constants.HttpConstant;
import com.mrd.shiptask_shipowner.serviecs.BizNetworkService;
import com.mrd.shiptask_shipowner.serviecs.initdatadao.InitDatasDaoImpl;
import com.mrd.shiptask_shipowner.shiareutil.SharePreferenceUtil;
import com.mrd.shiptask_shipowner.unit_tools.HttpUrlConstant;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ContentFragment extends Fragment {
	Button chuanyuanBnt, shenqingBnt, hetongBnt, qiandanBnt;
	ProgressDialog progressDialog;
	private ImageView newsBnt;
	private TextView newsbtTap;
	private SharePreferenceUtil sp;
	private View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		view = inflater.inflate(R.layout.fragment_content, container, false);
		// 刷新本地任务提醒数据
		progressDialog = new ProgressDialog(getActivity());
		sp = new SharePreferenceUtil(getActivity(), HttpConstant.SAVE_USER);
		// 刷新消息数
		getNewsCountDatas();
		initButtonView(view);
		return view;
	}

	private void initButtonView(View view) {
		// TODO Auto-generated method stub
		chuanyuanBnt = (Button) view.findViewById(R.id.chuanyuanBt);
		shenqingBnt = (Button) view.findViewById(R.id.shenqingBt);
		hetongBnt = (Button) view.findViewById(R.id.hetongBt);
		qiandanBnt = (Button) view.findViewById(R.id.qiandanBt);
		newsBnt = (ImageView) view.findViewById(R.id.newsBt);
		chuanyuanBnt.setOnClickListener(ocl);
		shenqingBnt.setOnClickListener(ocl);
		hetongBnt.setOnClickListener(ocl);
		qiandanBnt.setOnClickListener(ocl);
		newsBnt.setOnClickListener(ocl);
		newsbtTap = (TextView) view.findViewById(R.id.newsbtTap);
		newsbtTap.setVisibility(View.GONE);
		if (sp.getMessageCount().length() > 0
				&& !"0".equals(sp.getMessageCount())) {
			newsbtTap.setVisibility(View.VISIBLE);
			newsbtTap.setText(sp.getMessageCount() + "条未读");
		}

	}

	public final OnClickListener ocl = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			// 发布船源信息
			case R.id.chuanyuanBt:
				InitDatasDaoImpl.getShipOrderDatas(getActivity());
				break;
			// 货运申请
			case R.id.shenqingBt:
				InitDatasDaoImpl.getShipMakeToOrderDatas(getActivity());
				break;
			// 运输合同
			case R.id.hetongBt:
				InitDatasDaoImpl.getElectronMainifestDatas(getActivity());
				break;
			// 确认签单
			case R.id.qiandanBt:
				InitDatasDaoImpl.getConfirmOrderDatas(getActivity());
				break;
			// 查看信息
			case R.id.newsBt:
				sp.setMessageCount("");
				startActivity(new Intent(getActivity(), NewsActivity.class));
				break;
			default:
				break;
			}
		}
	};

	public void getNewsCountDatas() {
		// TODO Auto-generated method stub
		try {
			SharePreferenceUtil sp = new SharePreferenceUtil(getActivity(),
					HttpConstant.SAVE_USER);
			Map<String, Object> param2 = new HashMap<String, Object>();
			param2.put("ship", sp.getShip_id());
			param2.put("status", "0");
			new Thread(new BizNetworkService(getActivity(), handler,
					HttpUrlConstant.GET_MESSAGE_COUNT, param2)).start();
		} catch (Exception e) {
		}
	}

	/**
	 * 请求结果处理
	 */
	@SuppressLint("HandlerLeak")
	public Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			try {
				Bundle data = msg.getData();
				if (!data.isEmpty()) {
					try {
						switch (msg.what) {
						case HttpUrlConstant.GET_MESSAGE_COUNT:
							if (data.getBoolean("ispass")) {
								initButtonView(view);
							}
							break;
						default:
							break;
						}
					} catch (Exception e) {
					}
				}
			} catch (Exception e) {
			}
		};
	};
}
